/**
 emma_project
 Copyright (C) 2015  Rodrigo Garcia

 This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*

  SD GENERIC SOURCE FILE
 
  the purpouse of this file is to make easier the use of the SD
  card functions from included libraries in the header.
  To do so the following functions use the FAT and SD libraries
  to perform essential operations on the SD card and data storage.
  
  The main file should worry only on calling this file's funcitons
  to perform SD card funcitons, that's why when other SD, SPI
  or FAT library is used, it is mandatory to do changes here first.

*/

#include "SD_generic.h"
#include "SD_routines.h"
#include "FAT32.h"
#include "SPI_routines.h"

char SD_initialization(void)
{
  // It assumes spi module has been initializated
  char i, Error;
  for (i=10; i>0; i--)
    {
      Error = SD_init();
      if(!Error)
	{
	  Error = getBootSectorData (); //read boot sector and keep necessary data in global variables
	  if(!Error)
	    return 0;
	}
    }
  return Error;
}

void SD_list_files(void)
{
  findFiles(FAT32_GET_LIST, 0);
  return 0;
}

char SD_readFile(char *filename)
{
  readFile(FAT32_READ, filename);
  return 0;
}

char SD_writeFile(char *filename, char *contents)
{
  writeFile(filename, contents);
  return 0;
}

char SD_deleteFile(char *filename)
{
  deleteFile(filename);
  return 0;
}

/*
Fills the new_content_buffer[] with new data from *new_content, if append = 1 the buffer is filled from buffer_index postiion
  position, if append = 0 , the buffer is overwriten with *new_content
   and buffer_index value will be sizeof(*new_content)
 RETURNS 1 - When new_content string can't be stored complete in buffer[]
         (overflow because of size) NO content is copied to buffer[].
 RETURNS 0 - After copied all *new_content and new_content_buffer_index is less equal
             than 512, new_content_buffer[] contains *new_content after this.
*/
unsigned char addToNewContentBuffer(unsigned char *new_content, unsigned char append)
{
  if (num_cars_cad(new_content) + new_content_buffer_index >= 512)
    return 1;
  else
    {
      new_content_buffer_index += num_cars_cad(new_content);
      if(append == 1)
	  sprintf(new_content_buffer, "%s%s", new_content_buffer, new_content);
      if(append == 0)
	{
	  new_content_buffer_index= num_cars_cad(new_content);
	  sprintf(new_content_buffer, "%s", new_content);
	}
    }
  return 0;
}

unsigned char num_cars_cad(unsigned char *cad)
{
  int i = 0;
  while(*cad != '\0')
    {
      i++;
      *cad++;
    }
  return i;

}
